package cn.yunhe;

import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Created by zs on 2017/11/30.
 * 性能测试，操作速度
 */
public class Demo1 {

    @Test
    public void test1() {
//        Jedis jedis = new Jedis("localhost", 6379);
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        //最大空闲数
        poolConfig.setMaxIdle(50);
        //最大连接数
        poolConfig.setMaxTotal(100);
        //最大等待毫秒
        poolConfig.setMaxWaitMillis(20000);
        //使用配置创建连接池
        JedisPool pool = new JedisPool(poolConfig, "localhost");
        //从连接池中获取单个连接
        Jedis jedis = pool.getResource();
        //如果需要密码 jedis.auth("password");



        int i = 0;

        try {
            long start = System.currentTimeMillis(); //开始毫秒数
            while (true) {
                long end = System.currentTimeMillis();
                if (end - start >= 1000) {  //大于1秒，结束操作
                    break;
                }
                i++;
                jedis.set("test" + i, i + "");
            }
        } finally {
            jedis.close();
        }

        System.out.println("redis每秒操作" + i + "次");
    }
}
